import java.util.Arrays;

public class Test {

    public static int[] copy(int[] array, int newLen){
        int[] copy = new int[newLen];
        for(int i = 0; i < array.length; i++){
            copy[i] = array[i];
        }
        return copy;
    }
    public int[] merge(int[] nums1, int m, int[] nums2, int n) {
        int end1 = m - 1;
        int end2 = n - 1;
        nums1 = copy(nums1, m+n);
        int index = m+n-1;
        while(end1 >= 0 && end2 >= 0){
            if(nums1[end1] >= nums2[end2]){
                nums1[index--] = nums1[end1--];
            }else{
                nums1[index--] = nums2[end2--];
            }
        }

        while(end2 >= 0){
            nums1[index--] = nums2[end2--];
        }

        while(end1 >= 0){
            nums1[index--] = nums1[end1--];
        }

        return nums1;
    }

    public static void main(String[] args) {
        int[] nums1 = {1,2,3};
        int[] nums2 = {2,5,6};
        Test test = new Test();

        nums1 = test.merge(nums1, nums1.length, nums2, nums2.length);
        for (int i = 0; i < nums1.length; i++) {
            System.out.println(nums1[i]);
        }
    }
}
